package com.gzc.just.play.last.war.sceneserver.gameobject.unit;

import com.gzc.just.play.last.war.sceneserver.gameobject.base.GameObjectCamp;
import com.gzc.just.play.last.war.sceneserver.gameobject.base.GameObjectType;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 英雄类 - 玩家操控的单位
 * 
 * 对应C++ SSHero
 * 继承自GameUnit，具有英雄特有的属性和方法
 * 
 * @author AI Assistant
 * @version 1.0.0
 * @since 2025-11-22
 */
@Data
@lombok.EqualsAndHashCode(callSuper=false)
public class Hero extends GameUnit {
    
    private static final Logger log = LoggerFactory.getLogger(Hero.class);
    private static final long serialVersionUID = 1L;

    // 英雄属性
    private long userId;                // 用户ID
    private int heroTypeId;             // 英雄类型ID
    private String heroName;            // 英雄名称
    private String nickname;            // 玩家昵称
    
    // 等级和经验
    private int level;                  // 等级
    private long experience;            // 当前经验
    private long nextLevelExp;          // 下一级所需经验
    
    // 资源
    private int gold;                   // 金币
    private int gems;                   // 宝石
    private int goldSpent;              // 已消费的金币
    
    // 英雄状态
    private int killStreak;             // 连杀数
    private int maxKillStreak;          // 最多连杀数
    private int assistCount;            // 助攻数
    
    // 选择状态
    private boolean heroChoosed;        // 是否已选择英雄
    private boolean loadComplete;       // 是否加载完成
    private boolean offline;            // 是否离线
    
    // 队伍和位置
    private int teamId;                 // 队伍ID (1 or 2)
    private int battlePosition;         // 战斗位置（1-5）
    
    // 战斗能力值
    private double battlePower;         // 战斗力

    /**
     * 构造函数
     */
    public Hero(long objectId, long userId, int heroTypeId, String heroName, String nickname,
                GameObjectCamp camp, int maxHealth, int maxMana) {
        super(objectId, GameObjectType.HERO, camp, maxHealth, maxMana, 50, 10, 5);
        
        this.userId = userId;
        this.heroTypeId = heroTypeId;
        this.heroName = heroName;
        this.nickname = nickname;
        
        this.level = 1;
        this.experience = 0;
        this.nextLevelExp = 1000;
        
        this.gold = 0;
        this.gems = 0;
        this.goldSpent = 0;
        
        this.killStreak = 0;
        this.maxKillStreak = 0;
        this.assistCount = 0;
        
        this.heroChoosed = false;
        this.loadComplete = false;
        this.offline = false;
        
        this.teamId = 0;
        this.battlePosition = 0;
        
        this.battlePower = 100;
    }

    /**
     * 初始化英雄
     */
    @Override
    public void initialize() {
        super.initialize();
        log.info("初始化英雄: userId={}, heroId={}, heroName={}, nickname={}", 
                userId, heroTypeId, heroName, nickname);
    }

    /**
     * 销毁英雄
     */
    @Override
    public void destroy() {
        log.info("销毁英雄: userId={}, heroId={}", userId, heroTypeId);
        super.destroy();
    }

    /**
     * 处理心跳
     */
    @Override
    public void processHeartbeat(long currentTime, long deltaTime) {
        if (!isAlive() || offline) {
            return;
        }
        super.processHeartbeat(currentTime, deltaTime);
    }

    /**
     * 检查死亡状态
     */
    @Override
    protected void checkDeadState() {
        if (getCurrentHealth() <= 0 && !isDead()) {
            die();
        }
    }

    /**
     * 获得经验
     */
    public void gainExperience(long amount) {
        if (isDead()) {
            return;
        }

        experience += amount;
        log.debug("英雄 {} 获得经验 {}, 当前经验 {}/{}", 
                userId, amount, experience, nextLevelExp);

        // 检查升级
        while (experience >= nextLevelExp) {
            levelUp();
        }
    }

    /**
     * 升级
     */
    public void levelUp() {
        level++;
        experience -= nextLevelExp;
        nextLevelExp = (long) (nextLevelExp * 1.2); // 下一级需要更多经验

        // 提升属性
        setMaxHealth((int)(getMaxHealth() * 1.1));
        setMaxMana((int)(getMaxMana() * 1.1));
        setAttack(getAttack() * 1.05);
        setDefense(getDefense() * 1.05);

        log.info("英雄 {} 升级到 {} 级", userId, level);
    }

    /**
     * 获得金币
     */
    public void gainGold(int amount) {
        if (isDead()) {
            return;
        }

        gold += amount;
        log.debug("英雄 {} 获得金币 {}, 当前金币 {}", userId, amount, gold);
    }

    /**
     * 消费金币
     */
    public boolean spendGold(int amount) {
        if (amount < 0) {
            return false;
        }

        if (gold < amount) {
            log.warn("英雄 {} 金币不足, 需要 {}, 当前 {}", userId, amount, gold);
            return false;
        }

        gold -= amount;
        goldSpent += amount;
        log.debug("英雄 {} 消费金币 {}, 剩余 {}", userId, amount, gold);
        return true;
    }

    /**
     * 增加连杀
     */
    public void addKillStreak() {
        killStreak++;
        if (killStreak > maxKillStreak) {
            maxKillStreak = killStreak;
        }
        log.debug("英雄 {} 连杀数 {}", userId, killStreak);
    }

    /**
     * 重置连杀
     */
    public void resetKillStreak() {
        if (killStreak > 0) {
            log.debug("英雄 {} 连杀重置，最终连杀数 {}", userId, killStreak);
        }
        killStreak = 0;
    }

    /**
     * 增加助攻
     */
    public void addAssist() {
        assistCount++;
        log.debug("英雄 {} 助攻数 {}", userId, assistCount);
    }

    /**
     * 选择英雄
     */
    public void selectHero() {
        this.heroChoosed = true;
        log.info("英雄 {} 已选择", userId);
    }

    /**
     * 加载完成
     */
    public void completeLoading() {
        this.loadComplete = true;
        log.info("英雄 {} 加载完成", userId);
    }

    /**
     * 设置离线
     */
    public void setOffline(boolean offline) {
        this.offline = offline;
        if (offline) {
            log.info("英雄 {} 已离线", userId);
        } else {
            log.info("英雄 {} 已上线", userId);
        }
    }

    /**
     * 计算战斗力
     */
    public double calculateBattlePower() {
        double power = 0;
        
        // 基础属性贡献
        power += getMaxHealth() * 0.1;
        power += getMaxMana() * 0.05;
        power += getAttack() * 2.0;
        power += getDefense() * 1.5;
        power += getSpeed() * 1.0;
        
        // 等级贡献
        power += level * 10;
        
        // 金币贡献（代表装备）
        power += goldSpent * 0.5;
        
        this.battlePower = power;
        return power;
    }

    /**
     * 覆盖父类的die方法，添加英雄特定逻辑
     */
    @Override
    public void die() {
        super.die();
        resetKillStreak();
    }

    /**
     * 覆盖父类的revive方法，添加英雄特定逻辑
     */
    public void reviveHero(int healthPercent) {
        revive(healthPercent);
        killStreak = 0; // 复活时重置连杀
    }

    /**
     * 是否为英雄
     */
    @Override
    public boolean isHero() {
        return true;
    }

    @Override
    public String toString() {
        return String.format("Hero{id=%d, userId=%d, heroId=%d, heroName=%s, nickname=%s, level=%d, " +
                "hp=%d/%d, gold=%d, killStreak=%d, alive=%b, dead=%b}",
                getObjectId(), userId, heroTypeId, heroName, nickname, level,
                getCurrentHealth(), getMaxHealth(), gold, killStreak, !isDead(), isDead());
    }
}
